package com.marvels.financial.pay.hlbpay.dto.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.ServerProperties;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.util.StrUtil;

/**
 * 合利宝配置项
 * @author wujunfeng
 *
 */
@Component
public class HlbConfig {

	private static String configPropertiesPath;
	
	@Value("${config.properties.path}")
	public void setConfigPropertiesPath(String configPropertiesPath) {
		HlbConfig.configPropertiesPath = configPropertiesPath;
	}
	
	// 缓存，五分钟过期
	private static TimedCache<String, HlbConfig> cache = CacheUtil.newTimedCache(5 * 60 * 1000);
	
	public final static String DF = "DF";
	
	public final static String DK = "DK";
	
	/**
	 * 公钥地址
	 */
	private String certPath;
	/**
	 * 私钥地址
	 */
	private String pfxPath;
	/**
	 * 私钥密码
	 */
	private String pfxPwd;
	/**
	 * 商户号
	 */
	private String merchantNo;
	
	/**
	 * 请求地址
	 */
	private String requestUrl;

	/**
	 * 代付需要用到的key
	 */
	private String deskeyKey;
	
	/**
	 * 代付需要用到的私钥
	 */
	private String signKeyPrivate;
	
	/**
	 *  代付需要用到的key
	 */
	private String signkey;
	
	/**
	 * 代付需要用到的商户userid
	 */
	private String merchantUserId;
	
	/**
	 * 文件上传地址
	 */
	private String fileUploadUrl;
	
	
	public static HlbConfig getInstance(String type) {
		HlbConfig hlbConfig = cache.get(String.valueOf(type), false);
		if(hlbConfig != null){
			// 从缓冲中获取
			return hlbConfig;
		}
		
		// 拿到再缓存
		hlbConfig = new HlbConfig();
		if(StrUtil.equals(type, HlbConfig.DK))
		{
			/*hlbConfig.setCertPath("D:\\helipay.cer");
			hlbConfig.setPfxPath("D:\\C1800000002.pfx");
			hlbConfig.setPfxPwd("qwer1234");
			hlbConfig.setRequestUrl("http://test.trx.helipay.com/trx/quickPayApi/interface.action");;
			hlbConfig.setMerchantNo("C1800000002");*/
			hlbConfig.setCertPath(ServerProperties.getValue(configPropertiesPath, "common.properties", "hlb.cert.path"));
			hlbConfig.setPfxPath(ServerProperties.getValue(configPropertiesPath, "common.properties", "hlb.pfx.path"));
			hlbConfig.setPfxPwd(ServerProperties.getValue(configPropertiesPath, "common.properties", "hlb.pfx.pwd"));
			hlbConfig.setRequestUrl(ServerProperties.getValue(configPropertiesPath, "common.properties", "hlb.dk.request.url"));
			hlbConfig.setMerchantNo(ServerProperties.getValue(configPropertiesPath, "common.properties", "hlb.dk.merchant.no"));
			MarvelsLogUtil.getInstance().info("获取合利宝 - 代扣配置：" + JSONObject.toJSONString(hlbConfig));
		}else if(StrUtil.equals(type, HlbConfig.DF))
		{
			/*hlbConfig.setDeskeyKey("xglHJa0H1QKQi1Z9aaqnL0l2");
			hlbConfig.setSignKeyPrivate("MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAM+PcQN9JmrYJ+sKwI9kmBLAsBS6HHDT2Sgkh8BYbyTKovSMQat1II6l42HhUH6lr7bXwnRUw8I4qrNBZjz4cWZBi+vgkSL/1f1M/erW27t61DobvpgDpZQTtmQ7IDsiLuY7C7We+WwaBcddnju74ij3FPWCpgYBHGwdv5wwzRxdAgMBAAECgYBAzSreiPsujm/gDQpTeneUGz6eKgDpJOr+gnEzlyiUFwPLT+LM0hOpFZepHnxQHhB/CFu4kCJSB/kbYAa4cGSOlPo8zBLCfNajClZMLaKMAIb+0TmYNAnVcadC/4fXibzAW0zRS2/OK4H7wWUVEYyC66m+ieBaH5Jt/72+e6aYTQJBAPjjhGanLk22ml8i5+MzN94RBQStbGNxI6xtBXoKEIB2W/INPddZ877e7tknh+fVvctTZlE4Q5V1TT2ZL4wzke8CQQDVfaE9Cbc+aeg3Mb+Ap64tCK4WTHhWzHySN7VGTLdeF41ZjqTrIS7SSQyZOPOt/lMfFgXO0EnSdCqL+aexXFJzAkBeHyxi5bZNDVEzyS+IbEYkZKtRKYRj1tV2z4PSsxuqeRgsYXWRiyLye7w3wwtSUTKFQfTfojdsvf+H2/ZvPtFhAkAMygfctjZKAOIuXEaSmHjwrbJwF4il+n4D7F5ppbLeah7HnKn4g/ZgFowwqZ6/b5rfI9yZNRUXDGp4FC6di2BNAkB572zRbBT5Ot9mx9xVg6g/t0s3+LLEs1LBFEWQatRR9oC6qUzGNKTnZ/d5254ngnYXSRaQEZT698cJQV7kvmg4");
			hlbConfig.setSignkey("w1Eqi4G0yqQcnHwY7JJK4yYZaSYs2iAD");
			hlbConfig.setRequestUrl("http://test.trx.helipay.com/trx/entrustedLoan/interface.action");
			hlbConfig.setFileUploadUrl("http://test.trx.helipay.com/trx/entrustedLoan/upload.action");
			hlbConfig.setMerchantNo("C1800000002");*/
			hlbConfig.setDeskeyKey(ServerProperties.getValue(configPropertiesPath, "common.properties", "hlb.deskey.key"));
			hlbConfig.setSignKeyPrivate(ServerProperties.getValue(configPropertiesPath, "common.properties", "hlb.signKey.private"));
			hlbConfig.setSignkey(ServerProperties.getValue(configPropertiesPath, "common.properties", "hlb.signkey"));
			hlbConfig.setRequestUrl(ServerProperties.getValue(configPropertiesPath, "common.properties", "hlb.df.request.url"));
			hlbConfig.setFileUploadUrl(ServerProperties.getValue(configPropertiesPath, "common.properties", "hlb.file.upload.url"));
			hlbConfig.setMerchantNo(ServerProperties.getValue(configPropertiesPath, "common.properties", "hlb.df.merchant.no"));
			MarvelsLogUtil.getInstance().info("获取合利宝 - 代付配置：" + JSONObject.toJSONString(hlbConfig));
		}
		cache.put(type, hlbConfig);
		return hlbConfig;
	}

	public String getCertPath() {
		return certPath;
	}

	public void setCertPath(String certPath) {
		this.certPath = certPath;
	}

	public String getPfxPath() {
		return pfxPath;
	}

	public void setPfxPath(String pfxPath) {
		this.pfxPath = pfxPath;
	}

	public String getPfxPwd() {
		return pfxPwd;
	}

	public void setPfxPwd(String pfxPwd) {
		this.pfxPwd = pfxPwd;
	}

	public String getMerchantNo() {
		return merchantNo;
	}

	public void setMerchantNo(String merchantNo) {
		this.merchantNo = merchantNo;
	}

	public String getRequestUrl() {
		return requestUrl;
	}

	public void setRequestUrl(String requestUrl) {
		this.requestUrl = requestUrl;
	}

	public static String getConfigPropertiesPath() {
		return configPropertiesPath;
	}

	public String getDeskeyKey() {
		return deskeyKey;
	}

	public void setDeskeyKey(String deskeyKey) {
		this.deskeyKey = deskeyKey;
	}

	public String getSignKeyPrivate() {
		return signKeyPrivate;
	}

	public void setSignKeyPrivate(String signKeyPrivate) {
		this.signKeyPrivate = signKeyPrivate;
	}

	public String getSignkey() {
		return signkey;
	}

	public void setSignkey(String signkey) {
		this.signkey = signkey;
	}

	public String getMerchantUserId() {
		return merchantUserId;
	}

	public void setMerchantUserId(String merchantUserId) {
		this.merchantUserId = merchantUserId;
	}

	public String getFileUploadUrl() {
		return fileUploadUrl;
	}

	public void setFileUploadUrl(String fileUploadUrl) {
		this.fileUploadUrl = fileUploadUrl;
	}
	
}
